package org.rsbot.script.randoms;

import java.awt.Point;
import java.util.ArrayList;
import org.rsbot.script.Random;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.methods.Calculations;
import org.rsbot.script.methods.Camera;
import org.rsbot.script.methods.Game;
import org.rsbot.script.methods.Menu;
import org.rsbot.script.methods.Mouse;
import org.rsbot.script.methods.Objects;
import org.rsbot.script.methods.Tiles;
import org.rsbot.script.methods.Walking;
import org.rsbot.script.wrappers.RSObject;
import org.rsbot.script.wrappers.RSPath;
import org.rsbot.script.wrappers.RSPlayer;
import org.rsbot.script.wrappers.RSTile;

@ScriptManifest(authors={"Zenzie", "Iscream"}, name="Maze", version=1.5D)
public class Maze extends Random
{
  public ArrayList<Door> paths;
  String usePath;
  public char doorDir;
  public RSTile walkToTile;
  public int doorIndex;
  public int tryCount;
  public long lastTry;
  public RSTile lastDoor;

  public Maze()
  {
    this.paths = new ArrayList();

    this.usePath = "None";

    this.doorDir = 'a';
    this.walkToTile = null;
    this.doorIndex = 0;

    this.tryCount = 0;
    this.lastTry = 0L;
    this.lastDoor = null;
  }

  public void loadGreenPath()
  {
    this.paths.add(new Door(new RSTile(2903, 4555), 's', 0));
    this.paths.add(new Door(new RSTile(2890, 4566), 'e', 1));
    this.paths.add(new Door(new RSTile(2892, 4578), 'e', 2));
    this.paths.add(new Door(new RSTile(2894, 4567), 'e', 3));
    this.paths.add(new Door(new RSTile(2896, 4562), 'e', 4));
    this.paths.add(new Door(new RSTile(2905, 4561), 's', 5));
    this.paths.add(new Door(new RSTile(2915, 4560), 'n', 6));
    this.paths.add(new Door(new RSTile(2909, 4562), 'n', 7));
    this.paths.add(new Door(new RSTile(2924, 4566), 'w', 8));
    this.paths.add(new Door(new RSTile(2922, 4575), 'w', 9));
    this.paths.add(new Door(new RSTile(2916, 4568), 'n', 10));
    this.paths.add(new Door(new RSTile(2905, 4573), 'w', 11));
    this.paths.add(new Door(new RSTile(2906, 4585), 'n', 12));
    this.paths.add(new Door(new RSTile(2916, 4586), 's', 13));
    this.paths.add(new Door(new RSTile(2920, 4582), 'w', 14));
    this.paths.add(new Door(new RSTile(2910, 4582), 's', 15));
    this.paths.add(new Door(new RSTile(2910, 4572), 'n', 16));
    this.paths.add(new Door(new RSTile(2910, 4576), 'e', 17));
    log("Loaded green path");
  }

  public void loadBluePath()
  {
    this.paths.add(new Door(new RSTile(2891, 4588), 'w', 0));
    this.paths.add(new Door(new RSTile(2889, 4596), 'w', 1));
    this.paths.add(new Door(new RSTile(2893, 4600), 's', 2));
    this.paths.add(new Door(new RSTile(2901, 4598), 's', 3));
    this.paths.add(new Door(new RSTile(2897, 4596), 's', 4));
    this.paths.add(new Door(new RSTile(2894, 4587), 'e', 5));
    this.paths.add(new Door(new RSTile(2896, 4582), 'e', 6));
    this.paths.add(new Door(new RSTile(2898, 4570), 'e', 7));
    this.paths.add(new Door(new RSTile(2900, 4567), 'e', 8));
    this.paths.add(new Door(new RSTile(2911, 4566), 'n', 9));
    this.paths.add(new Door(new RSTile(2906, 4585), 'n', 10));
    this.paths.add(new Door(new RSTile(2916, 4586), 's', 11));
    this.paths.add(new Door(new RSTile(2920, 4582), 'w', 12));
    this.paths.add(new Door(new RSTile(2910, 4582), 's', 13));
    this.paths.add(new Door(new RSTile(2910, 4572), 'n', 14));
    this.paths.add(new Door(new RSTile(2910, 4576), 'e', 15));
    log("Loaded blue path");
  }

  public void loadCyanPath()
  {
    this.paths.add(new Door(new RSTile(2930, 4555), 's', 0));
    this.paths.add(new Door(new RSTile(2912, 4553), 's', 1));
    this.paths.add(new Door(new RSTile(2936, 4556), 'w', 2));
    this.paths.add(new Door(new RSTile(2934, 4568), 'w', 3));
    this.paths.add(new Door(new RSTile(2932, 4575), 'w', 4));
    this.paths.add(new Door(new RSTile(2930, 4561), 'w', 5));
    this.paths.add(new Door(new RSTile(2929, 4581), 'e', 6));
    this.paths.add(new Door(new RSTile(2930, 4590), 'w', 7));
    this.paths.add(new Door(new RSTile(2924, 4592), 's', 8));
    this.paths.add(new Door(new RSTile(2926, 4575), 'w', 9));
    this.paths.add(new Door(new RSTile(2924, 4583), 'w', 10));
    this.paths.add(new Door(new RSTile(2916, 4586), 's', 11));
    this.paths.add(new Door(new RSTile(2920, 4582), 'w', 12));
    this.paths.add(new Door(new RSTile(2910, 4582), 's', 13));
    this.paths.add(new Door(new RSTile(2910, 4572), 'n', 14));
    this.paths.add(new Door(new RSTile(2910, 4576), 'e', 15));
    log("Loaded cyan path");
  }

  public void loadPurplePath()
  {
    this.paths.add(new Door(new RSTile(2932, 4597), 'n', 0));
    this.paths.add(new Door(new RSTile(2921, 4599), 'n', 1));
    this.paths.add(new Door(new RSTile(2909, 4600), 's', 3));
    this.paths.add(new Door(new RSTile(2913, 4598), 's', 4));
    this.paths.add(new Door(new RSTile(2908, 4596), 's', 5));
    this.paths.add(new Door(new RSTile(2919, 4594), 's', 6));
    this.paths.add(new Door(new RSTile(2908, 4592), 's', 7));
    this.paths.add(new Door(new RSTile(2898, 4585), 'e', 8));
    this.paths.add(new Door(new RSTile(2903, 4588), 's', 9));
    this.paths.add(new Door(new RSTile(2902, 4575), 'e', 10));
    this.paths.add(new Door(new RSTile(2906, 4585), 'n', 11));
    this.paths.add(new Door(new RSTile(2916, 4586), 's', 12));
    this.paths.add(new Door(new RSTile(2920, 4582), 'w', 13));
    this.paths.add(new Door(new RSTile(2910, 4582), 's', 14));
    this.paths.add(new Door(new RSTile(2910, 4572), 'n', 15));
    this.paths.add(new Door(new RSTile(2910, 4576), 'e', 16));

    log("Loaded purple path");
  }

  public boolean activateCondition()
  {
    if (this.game.isLoggedIn()) if (this.objects.getNearest(new int[] { 3626, 3649 }) != null) {
        this.camera.setPitch(true);
        return true;
      }
    return false;
  }

  public int loop()
  {
    if (!activateCondition()) {
      return -1;
    }
    if (getMyPlayer().isMoving()) {
      return random(150, 200);
    }
    if ((getMyPlayer().getLocation().equals(new RSTile(2911, 4576))) && 
      (getMyPlayer().getAnimation() == -1)) {
      this.tiles.doAction(new RSTile(2912, 4576), "Touch");
      return random(1000, 1200);
    }

    if (this.usePath.equals("None")) {
      getPath();
      return random(900, 1100);
    }

    if (getMyPlayer().getLocation().equals(tileAfterDoor())) {
      this.doorIndex += 1;
      log("Getting new wall");
      getNewWall();
      return random(200, 300);
    }

    if ((this.walkToTile != null) && (this.calc.distanceTo(this.walkToTile) >= 3)) {
      if (!this.walking.getPath(this.walkToTile).traverse()) {
        this.walking.walkTileMM(this.walkToTile.randomize(2, 2));
      }
      return random(500, 600);
    }

    if ((this.walkToTile != null) && (this.calc.distanceTo(this.walkToTile) <= 3) && 
      (this.doorDir != 'a') && (!getMyPlayer().isMoving())) {
      if ((this.camera.getAngle() - turnCameraTo() < 30) || (this.camera.getAngle() - turnCameraTo() > 30)) {
        this.camera.setAngle(turnCameraTo());
      }
      RSObject localRSObject = this.objects.getTopAt(this.walkToTile, 4);
      if (((localRSObject != null) && (localRSObject.doAction("Open"))) || (atDoor(this.walkToTile, this.doorDir))) {
        return random(400, 500);
      }

    }

    return random(300, 350);
  }

  public void getNewWall() {
    for (Door localDoor : this.paths)
      if (localDoor.doorID == this.doorIndex) {
        this.walkToTile = new RSTile(localDoor.doorTile.getX(), localDoor.doorTile.getY());
        this.doorDir = localDoor.doorDir;
        localDoor.doorID = this.doorIndex;
        log("walking.walkTo: " + this.walkToTile.getX() + ", " + this.walkToTile.getY());
        log("Door index: " + this.doorIndex + " | Door direction: " + this.doorDir);
      }
  }

  public int turnCameraTo()
  {
    int i = this.doorDir;
    if (i == 97) {
      log("TURNCAMERATO: WALL DIRECTION IS 'A");
      return random(330, 380);
    }
    switch (i) {
    case 110:
      return random(330, 380);
    case 115:
      return random(155, 190);
    case 101:
      return random(245, 290);
    case 119:
      return random(65, 110);
    }
    return random(330, 380);
  }

  public RSTile tileAfterDoor() {
    int i = this.doorDir;
    if (i == 97) {
      log("TILEAFTERDOOR: doorD = A");
      return new RSTile(1, 1);
    }
    if (this.walkToTile == null) {
      log("TILEAFTERDOOR: walkToTile = NULL");
      return new RSTile(1, 1);
    }
    switch (i) {
    case 110:
      return new RSTile(this.walkToTile.getX(), this.walkToTile.getY() + 1);
    case 119:
      return new RSTile(this.walkToTile.getX() - 1, this.walkToTile.getY());
    case 101:
      return new RSTile(this.walkToTile.getX() + 1, this.walkToTile.getY());
    case 115:
      return new RSTile(this.walkToTile.getX(), this.walkToTile.getY() - 1);
    }
    return new RSTile(1, 1);
  }

  public void getPath() {
    int i = getMyPlayer().getLocation().getX();
    int j = getMyPlayer().getLocation().getY();
    if ((i >= 2920) && (i <= 2940) && (j >= 4572) && (j <= 4600)) {
      loadPurplePath();
      this.usePath = "purple";
      this.walkToTile = new RSTile(2932, 4597);
      this.doorDir = 'n';
      this.doorIndex = 0;
      log("Using purple path!");
    }
    if ((i >= 2891) && (i <= 2894) && (j >= 4586) && (j <= 4599)) {
      loadBluePath();
      this.usePath = "blue";
      this.walkToTile = new RSTile(2891, 4588);
      this.doorDir = 'w';
      this.doorIndex = 0;
      log("Using blue path!");
    }
    if ((i >= 2915) && (i <= 2933) && (j >= 4555) && (j <= 4560)) {
      loadCyanPath();
      this.usePath = "cyan";
      this.walkToTile = new RSTile(2930, 4555);
      this.doorDir = 's';
      this.doorIndex = 0;
      log("Using cyan path!");
    }
    if ((i >= 2891) && (i <= 2914) && (j >= 4555) && (j <= 4561)) {
      loadGreenPath();
      this.usePath = "green";
      this.walkToTile = new RSTile(2903, 4555);
      this.doorDir = 's';
      this.doorIndex = 0;
      log("Using green path!");
    }
  }

  public void onFinish()
  {
    log("Random event finished ~ Made By Zenzie");
  }

  public boolean atDoor(RSTile paramRSTile, char paramChar) {
    if (paramRSTile == null)
      return false;
    int i = paramRSTile.getX(); int j = paramRSTile.getY();
    int k = 0;
    switch (paramChar) {
    case 'N':
    case 'n':
      j++;
      break;
    case 'W':
    case 'w':
      i--;
      break;
    case 'E':
    case 'e':
      i++;
      break;
    case 'S':
    case 's':
      j--;
      break;
    default:
      k = 1;
    }
    if (k != 0)
      throw new IllegalArgumentException();
    return atDoorTiles(paramRSTile, new RSTile(i, j));
  }

  public boolean atDoorTiles(RSTile paramRSTile1, RSTile paramRSTile2) {
    if (paramRSTile1 != this.lastDoor) {
      this.lastTry = 0L;
      this.tryCount = 0;
      this.lastDoor = paramRSTile1;
    }
    this.tryCount += 1;
    if (System.currentTimeMillis() - this.lastTry > random(20000, 40000)) {
      this.tryCount = 1;
    }
    this.lastTry = System.currentTimeMillis();
    if (this.tryCount > 4) {
      if (random(0, 10) < random(2, 4)) {
        this.camera.setAngle(this.camera.getAngle() + (random(0, 9) < random(6, 8) ? random(-20, 20) : random(-360, 360)));
      }
      if (random(0, 14) < random(0, 2)) {
        this.camera.setPitch(random(0, 100));
      }
    }
    if (this.tryCount > 100) {
      log("Problems finding wall....");
      stopScript(false);
    }
    if ((!this.calc.tileOnScreen(paramRSTile1)) || (!this.calc.tileOnScreen(paramRSTile2)) || (this.calc.distanceTo(paramRSTile1) > random(4, 7))) {
      if (this.calc.tileOnMap(paramRSTile1)) {
        this.walking.getPath(paramRSTile1.randomize(3, 3)).traverse();
        sleep(random(750, 1250));
      } else {
        log("Cannot find wall tiles...");
        return false;
      }
    } else {
      ArrayList localArrayList = new ArrayList();
      localArrayList.add(paramRSTile1);
      localArrayList.add(paramRSTile2);
      try {
        Point[] arrayOfPoint = new Point[localArrayList.size()];
        for (int i = 0; i < localArrayList.size(); i++) {
          arrayOfPoint[i] = this.calc.tileToScreen((RSTile)localArrayList.get(i));
        }
        float f1 = 0.0F;
        float f2 = 0.0F;
        for (Object localObject2 : arrayOfPoint) {
          f1 = (float)(f1 + localObject2.getX());
          f2 = (float)(f2 + localObject2.getY());
        }
        ??? = new Point((int)(f1 / arrayOfPoint.length), (int)(f2 / arrayOfPoint.length) - random(0, 40));
        if ((((Point)???).x == -1) || (((Point)???).y == -1))
          return false;
        if (Math.sqrt(Math.pow(this.mouse.getLocation().getX() - ((Point)???).getX(), 2.0D) + Math.pow(this.mouse.getLocation().getY() - ((Point)???).getY(), 2.0D)) < random(20, 30)) {
          for (String str : this.menu.getItems()) {
            if ((!str.contains("Open")) || 
              (!this.menu.doAction("Open"))) continue;
            this.lastTry = 0L;
            this.tryCount = 0;
            return true;
          }

        }

        this.mouse.move((Point)???, 7, 7);
        if (this.menu.doAction("Open")) {
          this.lastTry = 0L;
          this.tryCount = 0;
          return true;
        }
      } catch (Exception localException) {
        return false;
      }
    }
    return false;
  }

  public class Door
  {
    public RSTile doorTile;
    public char doorDir;
    public int doorID;

    public Door(RSTile paramChar, char paramInt, int arg4)
    {
      this.doorTile = paramChar;
      this.doorDir = paramInt;
      int i;
      this.doorID = i;
    }
  }
}